package com.wxy.controller;

import com.wxy.config.jwtConfig;
import com.wxy.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author 478685194
 */
@Controller
@RequestMapping("login")
public class LoginController {
    private static final String MANAGER_NAME ="admin";
    private static final String MANAGER_PASSWORD ="123456";
    private final LoginService loginService;
    @Autowired
    public LoginController(LoginService userService) {
        this.loginService = userService;
    }

    @Operation(summary = "用户登录",description = "用户登录")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("log")
    public String log(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpServletResponse response){
        String string=loginService.login(username,password,model);
        String string1 = "login";
        if(string1.equals(string)){
            return string;
        }
        Cookie cookie = new Cookie("token",string);
        cookie.setPath("/");
        response.addCookie(cookie);
        return "hello";
    }

    @Operation(summary = "管理身份登录",description = "管理登录")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("manager")
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password,Model model,
                           HttpServletResponse response){
        if(username.equals(MANAGER_NAME) && password.equals(MANAGER_PASSWORD)){
            String string= jwtConfig.getToken(0);
            Cookie cookie = new Cookie("token",string);
            cookie.setPath("/");
            response.addCookie(cookie);
            return "allbook";
        }
        else{
            model.addAttribute("message","账号或密码错误");
            return "login";
        }
    }

    @Operation(summary = "用户注册",description = "用户注册功能")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("register")
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("email") String email,
                           Model model,HttpServletResponse response){
        String string=loginService.register(username,password,email,model);
        String string1 = "login";
        if(string.equals(string1)){
            return string;
        }
        Cookie cookie = new Cookie("token",string);
        // 设置 Cookie 的路径
        cookie.setPath("/");
        response.addCookie(cookie);
        return "hello";
    }
}
